package com.ouc.eddyAlgorithm.controller;


import com.ouc.common.annotation.Log;
import com.ouc.common.core.controller.BaseController;
import com.ouc.common.core.domain.AjaxResult;
import com.ouc.common.core.page.TableDataInfo;
import com.ouc.common.enums.BusinessType;
import com.ouc.common.utils.DateUtils;
import com.ouc.common.utils.poi.ExcelUtil;
import com.ouc.eddyAlgorithm.domain.VgForecast;
import com.ouc.eddyAlgorithm.service.IVgTrackService;
import com.ouc.eddyAlgorithm.service.TrackCalculationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import java.util.List;
import org.springframework.web.bind.annotation.*;


/**
 * vg涡旋计算最终Controller
 *
 * @author b504
 * @date 2023-03-09
 */
@Controller
@CrossOrigin
@RequestMapping("/system/vgTrack")
public class VgTrackController extends BaseController {
  private String prefix = "system/vgTrack";

  @Autowired
  private IVgTrackService vgTrackService;

  @Autowired
  private TrackCalculationService trackCalculationService;

//  @RequiresPermissions("system:vgForecast:view")
  @GetMapping()
  public String vgForecast() {
    return prefix + "/vgForecast";
  }

  /**
   * 查询vg涡旋计算最终列表
   */
//  @RequiresPermissions("system:vgForecast:list")
  @CrossOrigin
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo list(VgForecast vgTrack, @RequestParam(value = "startPageFlag", required = false, defaultValue = "true") boolean startPageFlag) {
    if(startPageFlag){
      startPage();
    }
    List<VgForecast> list = vgTrackService.selectVgTrackList(vgTrack);
    return getDataTable(list);
  }

  /**
   * 导出vg涡旋计算最终列表
   */
//  @RequiresPermissions("system:vgForecast:export")
  @Log(title = "vg涡旋计算最终", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(VgForecast vgTrack) {
    List<VgForecast> list = vgTrackService.selectVgTrackList(vgTrack);
    ExcelUtil<VgForecast> util = new ExcelUtil<VgForecast>(VgForecast.class);
    return util.exportExcel(list, "vg涡旋计算最终数据");
  }

  /**
   * 新增vg涡旋计算最终
   */
  @GetMapping("/add")
  public String add() {
    return prefix + "/add";
  }

  /**
   * 新增保存vg涡旋计算最终
   */
//  @RequiresPermissions("system:vgForecast:add")
  @Log(title = "vg涡旋计算最终", businessType = BusinessType.INSERT)
  @PostMapping("/add")
  @ResponseBody
  public AjaxResult addSave(VgForecast vgTrack) {
    return toAjax(vgTrackService.insertVgTrack(vgTrack));
  }

  /**
   * 修改vg涡旋计算最终
   */
//  @RequiresPermissions("system:vgForecast:edit")
  @GetMapping("/edit/{id}")
  public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
    VgForecast vgTrack = vgTrackService.selectVgTrackById(id);
    mmap.put("vgTrack", vgTrack);
    return prefix + "/edit";
  }

  /**
   * 修改保存vg涡旋计算最终
   */
//  @RequiresPermissions("system:vgForecast:edit")
  @Log(title = "vg涡旋计算最终", businessType = BusinessType.UPDATE)
  @PostMapping("/edit")
  @ResponseBody
  public AjaxResult editSave(VgForecast vgTrack) {
    return toAjax(vgTrackService.updateVgTrack(vgTrack));
  }

  /**
   * 删除vg涡旋计算最终
   */
//  @RequiresPermissions("system:vgForecast:remove")
  @Log(title = "vg涡旋计算最终", businessType = BusinessType.DELETE)
  @PostMapping("/remove")
  @ResponseBody
  public AjaxResult remove(String ids) {
    return toAjax(vgTrackService.deleteVgTrackByIds(ids));
  }

  /**
   * 获取追踪计算的涡旋数据
   * 通过比较vg_forecast表中的开始时间和结束时间之间的涡旋
   * 得到追踪的涡旋，同一个涡旋，eddy_id相同
   * 开始时间和结束时间是左闭右开
   * 包含开始时间和结束时间的前一天
   *
   * @param startDate 开始时间
   * @param endDate   结束时间
   * @param level     层级
   * @return
   */
  @RequestMapping("/getTrackCalculation")
  @Log(title = "vg涡旋计算最终", businessType = BusinessType.INSERT)
  @ResponseBody
  public TableDataInfo getTrackCalculation(@RequestParam(value = "startDate") String startDate, @RequestParam(value = "endDate") String endDate,
                                           @RequestParam(value = "level", defaultValue = "0") Integer level) {
    try {
      boolean trackingByTime = trackCalculationService.getTrackingByTime(DateUtils.parseDate(startDate), DateUtils.parseDate(endDate), level);
      System.out.println("trackingByTime = " + trackingByTime);
      return getDataTable(vgTrackService.selectAll());
    } catch (Exception e) {
      e.printStackTrace();
      return new TableDataInfo();
    }


  }
}
